﻿using System;
using System.Web.Mvc;
using Newtonsoft.Json;
using JasonSoft.ScriptAnnotations;
using Newtonsoft.Json.Serialization;
using System.Web.Http;


namespace $rootnamespace$.Extensions
{
    public static class HtmlHelperExtensions
    {
        public static string RenderScriptInitialization<T>(this HtmlHelper helper, T model, string moduleVariableName)
        {
            if (string.IsNullOrEmpty(moduleVariableName))
            {
                throw new ArgumentException("moduleVariableName cannot be null or empty");
            }

            var settings = new JsonSerializerSettings()
            {
                ContractResolver = GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ContractResolver ?? new DefaultContractResolver()          
            };

            settings.Converters.Add(new AnnotatedJsonConverter(typeof(T)));

            string json = JsonConvert.SerializeObject(model, Formatting.Indented, settings);

            return string.Format("var {0} = {1}", moduleVariableName, json);
        }
    }
}
